#include "bicyclemangerwindow.h"
#include <QStringList>
#include <QHeaderView>
#include "PlaceManger.h"
#include <QAction>
#include <QToolBar>
BicycleMangerWindow::BicycleMangerWindow(QWidget *parent) : QMainWindow(parent)
{
    this->setWindowTitle("单车管理");
    QAction *MangerAction = new QAction("单车调度",this);
    QToolBar *MangerBar = this->addToolBar("单车调度");
    QAction *FlushAction = new QAction("刷新",this);
    QToolBar *FlushBar = this->addToolBar("刷新");
    FlushBar->addAction(FlushAction);

    MangerBar->addAction(MangerAction);

    resize(800,500);

    this->MangerWid = new BicycleMangerWid();

    this->table = new QTableWidget(this);
    this->table->setColumnCount(3);
    this->table->setRowCount(100);
    this->table->resize(800,500);
    this->table->move(0,30);
    QStringList list;
    list<<"编号:"<<"地点名称"<<"单车数量";
    this->table->setHorizontalHeaderLabels(list);
    this->table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    this->table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//居中
    this->table->verticalHeader()->setVisible(false);
    this->tableDataUpdate();
    connect(MangerAction,&QAction::triggered,this,[=](){

        this->MangerWid->show();
    });
    connect(this->MangerWid,&BicycleMangerWid::Edited,this,[=](){
        this->tableDataUpdate();
    });

    connect(FlushAction,&QAction::triggered,this,[=](){
        this->tableDataUpdate();
    });
}

void BicycleMangerWindow::tableDataUpdate()
{
    PlaceManger PM;
    int len;
    Place *places =  PM.List(len);
    int sum = 0;
    for(int i = 0;i<len;i++)
    {
        if(strcmp(places[i].getName(),"")==0)
            continue;
        QTableWidgetItem *itemId = new QTableWidgetItem(QString::number(places[i].getId()));
        QTableWidgetItem *itemName = new QTableWidgetItem(QString(places[i].getName()));
        QTableWidgetItem *itemNum = new QTableWidgetItem(QString::number(places[i].getBicycleNum()));
        this->table->setItem(sum,0,itemId);
        this->table->setItem(sum,1,itemName);
        this->table->setItem(sum++,2,itemNum);
    }
}


